#include "StdAfx.h"
#include "Utility.h"
#include <xhash>

std::stringstream ssRM;
boost::mutex ssRMMutex;

std::stringstream ssCV;
boost::mutex ssCVMutex;

using namespace Pattern;


template<typename T>
const T toT(const std::string& str )
{
	T value;

	ssCVMutex.lock();
	ssCV.clear();
	ssCV<<str;
	ssCV>>value;
	ssCVMutex.unlock();

	return value;
}

template<typename T> 
const std::string toStr(const T& value )
{
	std::string ret;
	ssCVMutex.lock();
	ssCV.clear();
	ssCV<<value;
	ssCV>>ret;
	ssCVMutex.unlock();
	return ret;
}


const std::string Pattern::Utility::MakeCodeToString( Uint src )
{
		Uint code = src;

		char picks[4];

		picks[0]=(code&0xff000000)>>24;
		picks[1]=(code&0x00ff0000)>>16;
		picks[2]=(code&0x0000ff00)>>8;
		picks[3]=(code&0x000000ff);

		int begin = 0;
		for(int i=0;i<4;i++)
		{
			if(picks[i]!=0)
			{
				begin = i;
				break;
			}
		}

		int len = 4-begin;

		char *build = new char[len+1];

		for(int i=0;i<len;i++)
		{
			build[i]=picks[begin+i];
		}

		build[len]=0;
		
		return build;
}

Uint Pattern::Utility::HashString(const std::string& source )
{
	return stdext::hash_value(source);
}

/*
const Pattern::DataItem Pattern::Utility::RegexMatch( const std::string& source,const std::string& regex )
{
	boost::regex exp(regex); 
	std::string content = source;
	boost::smatch what;	
	
	DataItem di_match = "RegexMatch";
	if(boost::regex_match(content,what,exp))
	{		
		boost::smatch::iterator iter = what.begin();
		boost::smatch::iterator iend = what.end();		
		
		
		DI_MAKE_ATTRIBUTE(source,source,di_match);
		DI_MAKE_ATTRIBUTE(regex,regex,di_match);

		while(iter!=iend)
		{		
			DataItem subItem = "match";						

			ssRMMutex.lock();
			ssRM.clear();
			ssRM<<(*iter).str();
			std::string v = ssRM.str();
			ssRMMutex.unlock();

			DI_MAKE_ATTRIBUTE(value,v.c_str(),subItem);
			
			di_match.AddChild(subItem);

			iter++;
		}
	}

	return di_match;
}
*/


Uint Pattern::Utility::ToUint(const std::string& str )
{
	return toT<Uint>(str);
}

const std::string Pattern::Utility::ToString(const Uint value )
{
	return toStr<Uint>(value);
}

const std::string Pattern::Utility::ToString(const bool value )
{	
	if(value)
		return "true";
	else
		return "false";
}

const std::string Pattern::Utility::ToString(const char value )
{
	return toStr<char>(value);
}

const std::string Pattern::Utility::ToString(const int value )
{
	toStr<int>(0);
	return toStr<int>(value);
}

const std::string Pattern::Utility::ToString(const char* value )
{
	return value;
}

const std::string& Pattern::Utility::ToString(const std::string& value )
{
	return value;
}

 const std::string Pattern::Utility::ToString( const float value )
{
	return toStr<float>(value);
}

 const std::string Pattern::Utility::ToString( const ULong value )
{
	return toStr<ULong>(value);
}

 const std::string Pattern::Utility::ToString( const UShort value )
{
	return toStr<UShort>(value);
}

 const std::string Pattern::Utility::ToString( const Uchar value )
{
	return toStr<Uchar>(value);
}
bool Pattern::Utility::ToBool(const std::string& str )
{
	return toT<bool>(str);
}

char Pattern::Utility::Tochar(const std::string& str )
{
	return toT<char>(str);
}

int Pattern::Utility::ToInt(const std::string& str )
{
	return toT<int>(str);
}

 float Pattern::Utility::ToFloat( const std::string& str )
{
	return toT<float>(str);
}

 std::vector<std::string> Pattern::Utility::SpliteString( const std::string& source,const std::string& delim )
{
	size_t last = 0;
	size_t index=source.find_first_of(delim,last);
	std::vector<std::string> ret;
	while (index!=std::string::npos)
	{
		ret.push_back(source.substr(last,index-last));
		last=index+1;
		index=source.find_first_of(delim,last);
	}
	if (index-last>0)
	{
		ret.push_back(source.substr(last,index-last));
	}
	return ret;
}

 ULong Pattern::Utility::ToULong( const std::string& str )
{
	return toT<ULong>(str);
}

 UShort Pattern::Utility::ToUShort( const std::string& str )
{
	return toT<UShort>(str);
}

 Uchar Pattern::Utility::ToUchar( const std::string& str )
{
	return toT<Uchar>(str);
}